//
//  MyInfoController.m
//  Job
//
//  Created by 如此的你 on 2018/8/21.
//  Copyright © 2018年 桑赐相. All rights reserved.
//

#import "MyInfoController.h"
#import "MyAvatarTableCell.h"
#import "UpdateInfoController.h"
#import "UpdateAvatarController.h"

@interface MyInfoController ()
@property (nonatomic,strong) XLUserModel *user;
@property (nonatomic , strong) UIBarButtonItem *rightItem;
@end

@implementation MyInfoController
-(instancetype)initWithStyle:(UITableViewStyle)style{
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.mj_header = nil;
    self.tableView.mj_footer = nil;
    self.navigationItem.title = @"个人信息";
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 15, 0, 0);
    [self.tableView registerNib:[UINib nibWithNibName:@"MyAvatarTableCell" bundle:nil] forCellReuseIdentifier:@"MyAvatarTableCell"];
    self.rightItem = [[UIBarButtonItem alloc]initWithTitle:@"保存信息" style:UIBarButtonItemStylePlain target:self action:@selector(updateInfo)];
    self.rightItem.enabled = YES;
    self.navigationItem.rightBarButtonItem = self.rightItem;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.user = [XLUserModel getUserInfo];
    [self.dataSource removeAllObjects];
    [self.dataSource addObjectsFromArray:[UserModel getUserTableView]];
    [self reloadData];
}

- (void)updateInfo {
    
    XLAuthModel *auth = [XLAuthModel getAuth];
    
    XLUserModel *user = [XLUserModel getUserInfo];
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:[user mj_JSONObject]];
    dict[@"userId"] = auth.userID;
    
    NSString *url = @"/api//v4/user/resume";
    MBProgressHUD *hud = [MBProgressHUD showMessage:@"提交数据中..."];
    [HTTPTools POST:url parameters:dict callback:^(id result, NSString *msg, NSError *error) {
        [hud hideAnimated:YES];
        if (!error) {
            if (!msg) {
                [MBProgressHUD showSuccess:@"修改成功"];
            } else {
                [MBProgressHUD showError:msg];
            }
        }
    }];
}



#pragma mark - UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataSource.count + 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }else{
        NSArray *array = self.dataSource[section-1];
        return array.count;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        MyAvatarTableCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"MyAvatarTableCell" forIndexPath:indexPath];
        cell1.vC = self;
        cell1.user = self.user;
        return cell1;
    } else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
            cell.textLabel.font = Font16;
            cell.textLabel.textColor = HexRGB(0x444444);
            cell.detailTextLabel.font = Font14;
        }
        
        NSArray *array = self.dataSource[indexPath.section - 1];
        
        NSDictionary *dict = array[indexPath.row];
        cell.textLabel.text = dict[@"name"];
        cell.detailTextLabel.text = dict[@"value"] ? dict[@"value"] : @"未填写";
        
        if (![dict[@"key"] isEqualToString:@"phone"]) {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        
        return cell;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 80;
    }
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0) {
        
        UpdateAvatarController *avatarVc = [UpdateAvatarController new];
        avatarVc.user = self.user;
        
        [self pushViewController:avatarVc];
    }else{
        if (indexPath.section == 1 && indexPath.row == 0) {
            return;
        }
        UpdateInfoController *updateVc = [UpdateInfoController new];
        
        NSArray *array = self.dataSource[indexPath.section - 1];
        
        
        NSDictionary *dict = array[indexPath.row];
        updateVc.navigationItem.title = [NSString stringWithFormat:@"修改%@",dict[@"name"]];
        updateVc.text = dict[@"value"];
        updateVc.key = dict[@"key"];
        updateVc.placheholder = [NSString stringWithFormat:@"请输入%@",dict[@"name"]];
        [self pushViewController:updateVc];
    }
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10.0;
}





@end
